<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**----------------------------------------------
 * 邮件发送方法
 * 使用PHPMailer类
 * @param $to 目标
 * @param $title 标题
 * @param $content 内容
 */
function sendMail($to,$title,$content){
//    引入PHPMailer的类文件
    require_once (EXTEND_PATH.'/org/PHPMailer_v5.1/class.phpmailer.php');
    require_once(EXTEND_PATH.'/org/PHPMailer_v5.1/class.smtp.php');
//   读取常用的配置
    $email_from=config('msg.EMAIL_FROM');
    $email_from_name=config('msg.EMAIL_FROM_NAME');
    $email_uname=config('msg.EMAIL_UNAME');
    $email_pwd=config('msg.EMAIL_PWD');
    $email_port=config('msg.EMAIL_PORT');
    $email_host=config('msg.EMAIL_HOST');
    if(empty($email_host)||empty($email_uname)||empty($email_pwd)||empty($email_from)){
        return ['status'=>'error','message'=>'邮箱格式配置不完整'];
    }
//    实例化PHPMailer类
    $mail = new PHPMailer();
    // 设置为要发邮件
    $mail->IsSMTP();
    // 是否允许发送HTML代码做为邮件的内容
    $mail->IsHTML(TRUE);
    $mail->CharSet='UTF-8';
    // 是否需要身份验证
    $mail->SMTPAuth=TRUE;
    /*  邮件服务器上的账号是什么 -> 到163注册一个账号即可 */
    $mail->From=$email_from;
    $mail->FromName=$email_from_name;
    $mail->Host=$email_host;
    $mail->Username=$email_uname;
    $mail->Password=$email_pwd;
    $mail->Port = $email_port;
    // 收件人
    $mail->AddAddress($to);
    // 邮件标题
    $mail->Subject=$title;
    // 邮件内容
    $mail->Body=$content;
    //发送邮件,并返回状态代码
    if($mail->Send()){
        return ['status'=>'success','message'=>'邮件发送成功'];
    }else{
        $emailError=$mail->ErrorInfo;
        return ['status'=>'error','message'=>$emailError];
    }
}

/**
 * 生成唯一的随机码
 */
function unicode(){
    return md5(uniqid());
}

/**
 * 返回操作状态
 * @param string $status 状态
 * @param string $msg 消息
 * @param string $data 数据
 * @return array
 */
function returnStatus($status,$msg,$data=[]){
    //参数过滤,在后端过滤的必要不大
    $status=aop\safe\SafeInput::xssTrip($status);
    $msg=aop\safe\SafeInput::xssTrip($msg);
    empty($data)||$data=aop\safe\SafeInput::xssTrip($data);
    //返回数据
    return empty($data)?['status'=>$status,$msg=>$msg]:['status'=>$status,'msg'=>$msg,'data'=>$data];
}


function returnJson($status,$msg,$data=[]){
    return empty($data)?json(['status'=>$status,$msg=>$msg]):json(['status'=>$status,'msg'=>$msg,'data'=>$data]);
}
